package com.huxi.lang.object.support;

import java.lang.reflect.Array;
import java.util.List;

import com.huxi.lang.object.VIndex;
import com.huxi.lang.object.VMap;

public abstract class AbstractVMap implements VMap {

	public Object get(VIndex key) {
		Object kv = get(key.key);
		if (kv instanceof List) {
			@SuppressWarnings("rawtypes")
			List list = (List) kv;
			if (key.index < 0) {
				int index = key.index + list.size();
				return list.get(index);
			} else {
				return list.get(key.index);
			}
		} else if (kv != null) {
			if (kv.getClass().isArray()) {
				if (key.index < 0) {
					int index = key.index + Array.getLength(kv);
					return Array.get(kv, index);
				} else {
					return Array.get(kv, key.index);
				}
			}
		}
		return null;
	}

	public Object set(VIndex key, Object value) {
		Object rs = null;
		Object kv = get(key.key);
		if (kv instanceof List) {
			@SuppressWarnings("rawtypes")
			List list = (List) kv;
			if (key.index < 0) {
				int index = key.index + list.size();
				return list.get(index);
			} else {
				return list.get(key.index);
			}
		} else if (kv != null) {
			if (kv.getClass().isArray()) {
				if (key.index < 0) {
					int index = key.index + Array.getLength(kv);
					return Array.get(kv, index);
				} else {
					return Array.get(kv, key.index);
				}
			}
		}
		return rs;
	}
}
